/**
 * 
 */
package org.compiere.model;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.Level;

import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;

/**
 * @author A.Yerzhanov
 * 21.12.2012
 */
public class Mhrmtaxexemptions extends X_hrm_taxexemptions {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1476002870973988120L;
	
	/**	Static Logger	*/
	private static CLogger	s_log	= CLogger.getCLogger (Mhrmtaxexemptions.class);

	/**
	 * @param ctx
	 */
	public Mhrmtaxexemptions(Properties ctx) {
		super(ctx);
		
	}
	
	/**
	 * @param ctx
	 * @param hrm_taxexemptions_ID
	 * @param trxName
	 */
	public Mhrmtaxexemptions(Properties ctx, int hrm_taxexemptions_ID,
			String trxName) {
		super(ctx, hrm_taxexemptions_ID, trxName);
		
	}
	
	/**
	 * @param ctx
	 * @param rs
	 * @param trxName
	 */
	public Mhrmtaxexemptions(Properties ctx, ResultSet rs, String trxName) {
		super(ctx, rs, trxName);
		
	}

	/**  */
	public static BigDecimal getRate( String aidtype, Timestamp dt){
		
		if (aidtype=="")
			return new BigDecimal(0);
		
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		BigDecimal result = new BigDecimal(0);
		
		// Employee data from the card
		String sql_ = "SELECT RATE FROM HRM_TAXEXEMPTIONS \n " +
						 "WHERE AD_CLIENT_ID=" + Env.getAD_Client_ID(Env.getCtx()) + " \n " +
						   "AND HRM_MATERIALAIDTYPE='" + aidtype + "' \n " +
						   "AND " + DB.TO_DATE(dt) + " BETWEEN STARTDATE AND ENDDATE " ;
		try
		{
			pstmt = DB.prepareStatement(sql_,null);						
			rs = pstmt.executeQuery();
			if(rs.next())
				return rs.getBigDecimal(1);		
		}
		catch (SQLException e)
		{
			s_log.log(Level.SEVERE, "product", e);
		}
		finally
		{
			DB.close(rs, pstmt);
			rs = null; pstmt = null;
		}

		return result;
	}
	
	public static Mhrmtaxexemptions[] getExeptRec(String type) {
		
		if (type == "")
			return null;
		
		ArrayList<Mhrmtaxexemptions> list = new ArrayList<Mhrmtaxexemptions>();
				
		// 
		String sql_ = "SELECT * FROM HRM_SET_TAX WHERE AD_CLIENT_ID=" + Env.getAD_Client_ID(Env.getCtx()) +	" AND HRM_TAXTYPE='" + type + "' ";		 
		//
		 	PreparedStatement pstmt = null;
			ResultSet rs = null;
			try
			{
				pstmt = DB.prepareStatement(sql_.toString(),null);			
				rs = pstmt.executeQuery();	
				while(rs.next())
					list.add(new Mhrmtaxexemptions(Env.getCtx(), rs, null));
			}
			catch (SQLException e)
			{
				s_log.log(Level.SEVERE, "product", e);
			}
			finally
			{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}
			
		return list.toArray(new Mhrmtaxexemptions[list.size()]);
		
	}
	
	/* (non-Javadoc)
	 * @see org.compiere.model.PO#beforeSave(boolean)
	 */
	@Override
	protected boolean beforeSave(boolean newRecord) {
		
	
		return super.beforeSave(newRecord);
	}
	
}
